<template>
  <div>
    <div style="margin-bottom: 10px">
      <el-link v-if="flag" @click="flag = false" type="primary">去修改</el-link>
      <div v-else @click="flag = true">
        <span class="iconfont icon-fanhui" style="font-size: 28px; cursor: pointer"></span>
      </div>
      <el-link v-if="flag" @click="openUpdatePassword" style="margin-left: 10px; color: #e6a23c" type="primary">修改密码</el-link>
    </div>
    <el-descriptions class="personForm" v-if="flag" direction="vertical" :column="3" border>
      <el-descriptions-item label="用户名">{{ person.name }}</el-descriptions-item>
      <el-descriptions-item label="用户编号">{{ person.uid }}</el-descriptions-item>
      <el-descriptions-item label="联系电话">{{ person.phone }}</el-descriptions-item>
      <el-descriptions-item label="邮箱">{{ person.email }}</el-descriptions-item>
      <el-descriptions-item label="角色">
        <el-tag size="small">{{ person.role }}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="性别">{{ person.gender }}</el-descriptions-item>
      <el-descriptions-item label="创建时间">{{ person.create_time }}</el-descriptions-item>
      <el-descriptions-item label="地址" :span="2"></el-descriptions-item>
      <el-descriptions-item label="用户头像">
        <el-avatar shape="square" :size="100" fit="cover" :src="person.avatar"></el-avatar>
      </el-descriptions-item>
    </el-descriptions>

    <el-form style="height: 500px" v-else :model="person" :rules="rules" class="personForm" ref="ruleForm" label-width="100px">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="person.name"></el-input>
      </el-form-item>
      <el-form-item label="用户编号">
        <el-input v-model="person.uid" disabled></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input required v-model="person.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="person.email"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="person.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户头像" prop="avatar">
        <el-upload class="avatar-uploader" :action="this.$upload" :on-success="uploadFileSuccess" :show-file-list="false">
          <img v-if="person.avatar != ''" :src="person.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="onSubmit('ruleForm')">修改</el-button>
      </el-form-item>
    </el-form>

    <el-dialog width="30%" title="修改密码" :close-on-click-modal="false" :append-to-body="true" :visible.sync="innerVisible">
      <el-form ref="passwordRule" :rules="passwordRules" :model="updatePassword" label-width="100px">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input show-password v-model="updatePassword.oldPassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input show-password v-model="updatePassword.newPassword"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="againPassword">
          <el-input show-password v-model="updatePassword.againPassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="editPassword('passwordRule')">确认修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { modifyPassword } from '@/api/adminApi.js'
export default {
  name: 'Person',
  data() {
    //邮箱验证
    var emailRule = (rule, value, callback) => {
      let patt = new RegExp('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$')
      if (!patt.test(value) && value != null) {
        callback(new Error('邮箱格式不对,请重新输入'))
      } else {
        callback()
      }
    }
    //手机号验证
    var phoneRule = (rule, value, cb) => {
      let patt = new RegExp('^1[3-9]\\d{9}$')
      if (!patt.test(value)) {
        cb(new Error('手机号格式不对，请重新输入'))
      } else {
        cb()
      }
    }

    return {
      flag: true,
      innerVisible: false,
      updatePassword: {
        uid: '',
        oldPassword: '',
        newPassword: '',
        againPassword: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 9, message: '长度在 3 到 9 个字符', trigger: 'blur' },
        ],
        email: [{ validator: emailRule, trigger: 'blur' }],
        phone: [
          { require: true, message: '请输入联系电话', trigger: 'blur' },
          { validator: phoneRule, trigger: 'blur' },
        ],
      },
      passwordRules: {
        oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
        newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
        againPassword: [{ required: true, message: '请再次输入新密码', trigger: 'blur' }],
      },
    }
  },
  props: ['person'],
  created() {
    this.flag = true
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('editAdmin', this.person)
        } else {
          this.$message.warning('请按表单要求填写完整！！！')
          return false
        }
      })
    },
    //修改密码
    editPassword(forName) {
      this.$refs[forName].validate((vaild) => {
        if (vaild) {
          this.updatePassword.uid = this.person.uid
          modifyPassword(this.updatePassword).then((res) => {
            if (res.data.code == 200) {
              this.$message.success(res.data.msg)
              sessionStorage.clear()
              this.$router.push('/login')
            }
          })
        } else {
          this.$message.error('请按照表单要求填写')
        }
      })
    },
    //图片上传成功
    uploadFileSuccess(res) {
      this.person.avatar = res.data.url
    },
    openUpdatePassword() {
      this.innerVisible = true
    },
  },
}
</script>

<style lang="less" scoped>
.personForm {
  height: 450px;
  overflow-y: scroll;
  background: none;
}
::-webkit-scrollbar {
  width: 0 !important;
}
::-webkit-scrollbar {
  width: 0 !important;
  height: 0;
}
</style>